﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace Innovation
{
    public class DefaultMaterial : Material
    {
        public virtual Matrix World
        {
            get { return (Matrix)GetParameterValue("World", typeof(Matrix)); }
            set { SetParameter("World", value); }
        }

        public virtual Matrix View
        {
            get { return (Matrix)GetParameterValue("View", typeof(Matrix)); }
            set { SetParameter("View", value); }
        }

        public virtual Matrix Projection
        {
            get { return (Matrix)GetParameterValue("Projection", typeof(Matrix)); }
            set { SetParameter("Projection", value); }
        }

        internal override void setMeshPart(IEModel Model, ModelMeshPart meshPart)
        {
 	        base.setMeshPart(Model, meshPart);

            Texture = ((string[])meshPart.Tag)[1];
        }

        string textureName;
        public virtual string Texture
        {
            get { return textureName; }
            set
            {
                textureName = value;
                texture = Engine.Content.Load<Texture2D>(value);
                SetParameter("Texture", texture);
                ((string[])this.MeshPart.Tag)[1] = value;
            }
        }

        Texture2D texture;
        public Texture2D TextureValue
        {
            get { return texture; }
        }

        public override void Update()
        {
            if (Engine.Services.ContainsService(typeof(Camera)))
            {
                Camera camera = Engine.Services.GetService<Camera>();

                View = camera.View;
                Projection = camera.Projection;
            }
        }

        public override void SaveSerializationData(SerializationData Data)
        {
            base.SaveSerializationData(Data);

            Data.AddData("DefaultMaterial.Texture", this.Texture);
        }

        public override void LoadFromSerializationData(SerializationData Data)
        {
            base.LoadFromSerializationData(Data);

            this.Texture = Data.GetData<string>("DefaultMaterial.Texture");
        }
    }
}
